package sorting;

import java.util.Arrays;

public class CountingSort {
	public static short[] sort(short[] numbers, short size)
	{
		int[] count = new int[size];
		for(int i = 0, length = numbers.length; i < length; i ++ )
		{
			count[numbers[i]] ++;
		}
		
		for(int i = 1, length = count.length; i < length; i ++)
		{
			count[i] = count[i] + count[i - 1];
		}
		
		short[] sorted = new short[numbers.length];
		
		for(int i = numbers.length - 1; i >= 0; i --)
		{
			sorted[-- count[numbers[i]]] = numbers[i];
		}
		
		return sorted;
	}
	
	public static void main(String[] args) {
		short[] numbers = {1, 1, 3, 1, 3, 4, 2, 5, 3, 2, 0, 2, 0, 5};
		System.out.println(Arrays.toString(sort(numbers, (short)6)));
	}
}
